import sys, re, operator

hijack_ases = set()
hijack_seg = []
hijack_file = open('./hijackings.format')
id = 0
for l in hijack_file:
  c = l[:-1].split('|') # time, pfx, home, bad_path, is_hijk
  if c[3]:
    ases = c[3].split(' ')
    hijack_seg.append(ases)
    for a in ases:
      hijack_ases.add(a)
hijack_file.close()

import requests
get_req = requests.session()
asurl = 'http://bgp.he.net/AS'
asseturl = 'http://bgp.he.net/irr/as-set/'
all_asset = set()

def down_asset(asset):
  try:
    get_resp = get_req.get('%s%s' % (asseturl, asset), timeout=60)
    if get_resp and get_resp.text:
      irr_match = re.match(
          ".+<span class='irrdata'>(.+)</span>",
          get_resp.text, re.DOTALL)
      if irr_match:
        irr = re.sub('<[^<]+>', '', irr_match.group(1))
        irr_lines = irr.split('\n')
        irf = open('./irr/%s' % asset, 'w')
        for l in irr_lines:
          c = None
          if l.startswith('member'):
            irf.write(l)
            c = l.split(' ')
            for v in c:
              if (v.startswith('AS-') or v.startswith('as-')) and (v not in all_asset):
                all_asset.add(v)
                down_asset(v)
        irf.close()
        print >> sys.stderr, "Done: %s" % asset
  except Exception as e:
    print >> sys.stderr, "Error:", e

for a in hijack_ases:
  try:
    get_resp = get_req.get('%s%s' % (asurl, a), timeout=60)
    if get_resp and get_resp.text:
      irr_match = re.match(
          ".+<span class='irrdata'>(.+)</span>",
          get_resp.text, re.DOTALL)
      if irr_match:
        irr = re.sub('<[^<]+>', '', irr_match.group(1))
        irr_lines = irr.split('\n')
        irf = open('./irr/AS%s' % a, 'w')
        for il in xrange(len(irr_lines)):
          l = irr_lines[il]
          if il+1<len(irr_lines) and re.match(' +action ', irr_lines[il+1]):
            l = l + irr_lines[il+1]
            if il+2<len(irr_lines) and (re.match(' +accept', irr_lines[ir+2])
                or re.match(' +announce', irr_lines[ir+2])):
              l = l + irr_lines[il+2]
          c = None
          if l.startswith('import: '):
            irf.write(l)
            ib = l.rindex('accept ')
            if l[-1] == ';':
              c = l[ib+7:-1].split(' ')
            else:
              c = l[ib+7:].split(' ')
          elif l.startswith('export: '):
            irf.write(l)
            ib = l.rindex('announce ')
            if l[-1] == ';':
              c = l[ib+9:-1].split(' ')
            else:
              c = l[ib+9:].split(' ')
          if c:
            for v in c:
              if (v.startswith('AS-') or v.startswith('as-')) and (v not in all_asset):
                all_asset.add(v)
                down_asset(v)
            del c[:]
        irf.close()
        print >> sys.stderr, "Done: AS%s" % a
  except Exception as e:
    print >> sys.stderr, "Error:", e

